package com.laiyima.zhongjiang.linghuilv.demo.home;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.laiyima.zhongjiang.linghuilv.demo.R;
import com.laiyima.zhongjiang.linghuilv.demo.Serviceprovider.BanlanceDetActivity;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.CenterPopupView;

import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.math.BigDecimal;
import java.text.DecimalFormat;

import me.imid.swipebacklayout.lib.app.SwipeBackActivity;

public class MyWalletActivity extends SwipeBackActivity implements View.OnClickListener {
     private LinearLayout myIncomeDetails,myBalance;
    private LinearLayout mImageView;
    private TextView show_money,show_MyWallet_count,to_look_banlance;
    private String TAG="MyWalletActivity";
    private int myCode,qrCode;

    protected boolean useThemestatusBarColor = false;//是否使用特殊的标题栏背景颜色，android5.0以上可以设置状态栏背景色，如果不使用则使用透明色值
    protected void setStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0及以上
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            //根据上面设置是否对状态栏单独设置颜色
            if (useThemestatusBarColor) {
                // getWindow().setStatusBarColor(getResources().getColor(R.color.colorTheme));
            } else {
                getWindow().setStatusBarColor(Color.TRANSPARENT);
            }
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4到5.0
            WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ) {//android6.0以后可以对状态栏文字颜色和图标进行修改
            getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_wallet);
        myBalance= (LinearLayout) findViewById(R.id.balance);
        myIncomeDetails= (LinearLayout) findViewById(R.id.incomedetails);
        mImageView= (LinearLayout) findViewById(R.id.mwhh_back);
        show_money= (TextView) findViewById(R.id.my_moeny_balance);
        show_MyWallet_count= (TextView) findViewById(R.id.show_MyWallet_count);
        to_look_banlance= (TextView) findViewById(R.id.to_look_banlance);
        to_look_banlance.setOnClickListener(this);

        setStatusBar();
        myBalance.setOnClickListener(this);
        myIncomeDetails.setOnClickListener(this);
        mImageView.setOnClickListener(this);
        intView();
    }

    @Override
    public void onRestart() {
        intView();
        super.onRestart();
    }



    private void intView() {
        SharedPreferences sharedPreferences =getSharedPreferences("userinfo.txt", Context.MODE_PRIVATE);
        String userID = sharedPreferences.getString("userID", "");
        RequestParams params = new RequestParams("http://zljl.laiima.com/index.php?s=/api/Wallet/balance");
        //需要带入的参数
        params.addBodyParameter("uid", userID);
        // params.addBodyParameter("captcha", captcha);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                Log.e(TAG, "回调成功");
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    myCode = jsonObject.optInt("code", 0);
                    String money=jsonObject.optString("money","");
                    String money_balance=jsonObject.optString("count_income","");
                    String freeze=jsonObject.optString("freeze","");


                    Double zz = Double.parseDouble(money_balance);
                    DecimalFormat decimalFormat1 = new DecimalFormat("0.00");
                    String xx = decimalFormat1.format(zz);
                    BigDecimal bgg=new BigDecimal(xx);
                    show_MyWallet_count.setText(bgg+"");

                    Double z = Double.parseDouble(money);
                    Double zq = Double.parseDouble(freeze);
                    Double ad=z+zq;

                    DecimalFormat decimalFormat = new DecimalFormat("0.00");
                    String x = decimalFormat.format(ad);
                    BigDecimal bg=new BigDecimal(x);
                    show_money.setText(bg+"");


                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (myCode == 200) {

                }
                if (myCode == 500) {
                    Toast.makeText(getBaseContext().getApplicationContext(), "系统异常", Toast.LENGTH_SHORT).show();
                    Log.e(TAG, "系统异常");
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }
            @Override
            public void onCancelled(CancelledException cex) {

            }
            @Override
            public void onFinished() {

            }
        });


    }


    @Override
    public void onClick(View v) {
        int id=v.getId();
        switch (id){
            case R.id.to_look_banlance:
                Intent BBintent = new Intent(MyWalletActivity.this, BanlanceDetActivity.class);
                startActivity(BBintent);
                break;
            case R.id.mwhh_back:
                 finish();
                break;
            case R.id.balance:
    //            queryreal();
                Intent Balanceintent=new Intent(MyWalletActivity.this,BalanceActivity.class);
                startActivity(Balanceintent);

                break;
            case R.id.incomedetails:
                Intent IDintent=new Intent(MyWalletActivity.this,IncomeDetailsActivity.class);
                startActivity(IDintent);

                break;
        }

    }

    private void dialog_show() {
        MyWalletActivity.CustomPopup customPopup = new MyWalletActivity.CustomPopup(this);
        new XPopup.Builder(this)
                .asCustom(customPopup)
                .show();
    }
    public  class CustomPopup extends CenterPopupView {
        public CustomPopup(@NonNull Context context) {
            super(context);
        }
        @Override
        protected int getImplLayoutId() {
            return R.layout.toname_popup;
        }
        @Override
        protected void onCreate() {
            super.onCreate();

            findViewById(R.id.toName_close).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
            findViewById(R.id.toName_dialog_button).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent IDintent=new Intent(MyWalletActivity.this, NameActivity.class);
                    startActivity(IDintent);
                }
            });
        }
        protected void onShow() {
            super.onShow();
        }
    }


    public void queryreal(){
        SharedPreferences sharedPreferences =getSharedPreferences("userinfo.txt", Context.MODE_PRIVATE);
        String userID = sharedPreferences.getString("userID", "");
        RequestParams params = new RequestParams("http://zljl.laiima.com/index.php?s=/lfl_api/Commonlfl/query_real");
        //需要带入的参数
        params.addBodyParameter("uid", userID);
        Log.e(TAG, "回调成功userID"+userID);
        // params.addBodyParameter("captcha", captcha);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                Log.e(TAG, "回调成功");
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    qrCode = jsonObject.optInt("code", 0);
                    Log.e(TAG, "回调成功qrCode"+qrCode);

                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (qrCode == 200) {
                     Intent Balanceintent=new Intent(MyWalletActivity.this,BalanceActivity.class);
                    startActivity(Balanceintent);
                }
                if (qrCode == 201) {
                    dialog_show();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }
            @Override
            public void onCancelled(CancelledException cex) {

            }
            @Override
            public void onFinished() {

            }
        });

    }

}
